﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Utilities;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.UI;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Navigation;

namespace DR5.SPExtend
{
    public static class SPXUtility
    {
        // Microsoft.SharePoint.Utilities.SPUtility
        public static string GetRedirectUrl(HttpRequest request)
        {
            string text = request.QueryString["Source"];
            if (!SPUrlUtility.IsProtocolAllowed(text))
            {
                text = string.Empty;
            }
            if (!string.IsNullOrEmpty(text))
            {
                text = SPHttpUtility.UrlPathDecode(text, true);
                if (!SPUtility.RedirectValidate(text, HttpContext.Current))
                {
                    text = null;
                }
            }
            return text;
        }

        public static void CloseSPModalDialog(HttpResponse response)
        {
            response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
        }

        public static void WriteLog(string section, string message)
        {
            SPDiagnosticsService.Local.WriteEvent(0, new SPDiagnosticsCategory(section, TraceSeverity.Medium, EventSeverity.Warning), EventSeverity.Warning, message);
        }

        public static void MakeBlankHeaderNode(this SPNavigationNode node)
        {
            try
            {
                node.Properties["BlankUrl"] = "True";
                node.Properties["LastModifiedDate"] = DateTime.Now;
                node.Properties["TaDR5et"] = "";
                node.Properties["vti_navsequencechild"] = "true";
                node.Properties["UrlQueryString"] = "";
                node.Properties["CreatedDate"] = DateTime.Now;
                node.Properties["Description"] = "";
                node.Properties["UrlFragment"] = "";
                node.Properties["NodeType"] = "Heading";
                node.Properties["Audience"] = "";
                node.Update();
            }
            catch (Exception ex)
            {

            }
        }
    }
}
